package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * 年审添加车辆
 * @author kuanter
 *
 */
public class NSAddCar extends BaseActivity_NoBar {
	Button btTopLeft_xgcl;
	Button bt_done;
	
	RelativeLayout chosecp;
	RelativeLayout chosecx;
	
	TextView tv_clxx;
	TextView tv_tclx;
	
	public static final int cp_request = 100;
	public static final int cx_request = 2;
	
	String userID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ns_addcar);
		btTopLeft_xgcl = (Button) findViewById(R.id.btTopLeft_xgcl);
		bt_done = (Button) findViewById(R.id.bt_done);
		
		tv_clxx = (TextView) findViewById(R.id.tv_clxx);
		tv_tclx = (TextView) findViewById(R.id.tv_tclx);
		
		Intent intent = getIntent();
		userID = intent.getStringExtra("userID");
		
		btTopLeft_xgcl.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		bt_done.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//TODO
				if(tv_clxx.getText().toString().equals("　请选择")) {
					showToast("请选择车牌");
				} else if(tv_tclx.getText().toString().equals("　请选择")) {
					showToast("请选择车型");
				} else {
					showProgress("添加车辆中...");
					DataLoader addCar = new DataLoader(NSAddCar.this);
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					NameValuePair userId = new BasicNameValuePair("userId",userID);
					NameValuePair vehicleBrand = new BasicNameValuePair("vehicleBrand",pp);
					NameValuePair vehicleModels = new BasicNameValuePair("vehicleModels",xh);
					NameValuePair licensePlateNumber = new BasicNameValuePair("licensePlateNumber",tv_clxx.getText().toString());
					params.add(userId);
					params.add(vehicleBrand);
					params.add(vehicleModels);
					params.add(licensePlateNumber);
					addCar.DisplayData(MainConstants.HOST_test + HttpConfigAPI.addCar, params, addCarHandler, "", false);
				}
			}
		});
		
		chosecp = (RelativeLayout) findViewById(R.id.choseCar);
		chosecp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent_cp = new Intent(NSAddCar.this, TjclActivity.class);
				intent_cp.putExtra("cp", tv_clxx.getText().toString());
				startActivityForResult(intent_cp, cp_request);
			}
		});
		chosecx = (RelativeLayout) findViewById(R.id.chosetc);
		chosecx.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent_cx = new Intent(NSAddCar.this, ClppActivity.class);
				startActivityForResult(intent_cx, cx_request);
			}
		});
		
	}
	
	String pp;  //品牌
	String xh;	//型号
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == cp_request) {
			if(resultCode == 0 && data != null) {
				String cp = data.getStringExtra("cp");
				tv_clxx.setText(cp);
			}
		} else if(resultCode == cx_request) {
			if(data != null) {
				pp = data.getStringExtra("pp");
				xh = data.getStringExtra("xh");
				tv_tclx.setText(pp + "－" + xh);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}


	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}

	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		super.onResume();
	}


	public void exit(View view) {
		finish();
	}
	
	
	Handler addCarHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == HttpTask.RESULT_OK) {
				finishProgress();
				String json = (String) msg.obj;
				try {
					if(StringUtil.isSuccess(json)) {
						JSONObject jo = new JSONObject(json);
						String vehicleId = jo.optString("vehicleId");
						Intent intent = new Intent();
						intent.putExtra("carCard", tv_clxx.getText().toString());  //+ "—" 
						intent.putExtra("carInfo_pp", pp);
						intent.putExtra("carInfo_xh", xh);
						intent.putExtra("vehicleId", vehicleId);
						setResult(0, intent);
						finish();
					} else {
						showToast("添加失败，请稍后再试");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else if(msg.what == HttpTask.RESULT_ERROR) {
				showToast("添加失败，请稍后再试");
			}
			bt_done.setClickable(true);
			super.handleMessage(msg);
		}
		
	};

	
}
